package cz.muni.fi.pa165.travelagency.web;

import com.samaxes.stripes.ejb3.EJBBean;
import cz.muni.fi.pa165.travelagency.api.entities.IPackageTour;
import cz.muni.fi.pa165.travelagency.api.entities.ITour;
import cz.muni.fi.pa165.travelagency.api.services.IPackageTourService;
import cz.muni.fi.pa165.travelagency.api.services.ITourService;
import cz.muni.fi.pa165.travelagency.dto.PackageTourDTO;
import cz.muni.fi.pa165.travelagency.dto.TourDTO;
import java.util.List;
import javax.ejb.EJB;
import net.sourceforge.stripes.action.ActionBean;
import net.sourceforge.stripes.action.ActionBeanContext;
import net.sourceforge.stripes.action.Before;
import net.sourceforge.stripes.action.ForwardResolution;
import net.sourceforge.stripes.action.RedirectResolution;
import net.sourceforge.stripes.action.Resolution;
import net.sourceforge.stripes.action.UrlBinding;
import net.sourceforge.stripes.controller.LifecycleStage;
import net.sourceforge.stripes.validation.Validate;
import net.sourceforge.stripes.validation.ValidateNestedProperties;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 *
 * @author Petr Stribny
 */
@UrlBinding("/tour/{$event}/{tour.id}")
public class TourActionBean implements ActionBean {
    
    final static Logger log = LoggerFactory.getLogger(PackageTourActionBean.class);
    private ActionBeanContext context;
    
    @ValidateNestedProperties(value = {
        @Validate(on = {"add", "save"}, field = "name", required = true),
        @Validate(on = {"add", "save"}, field = "pricePerPerson", required = true, minvalue = 0),
        @Validate(on = {"add", "save"}, field = "date", required = true),
        @Validate(on = {"add", "save"}, field = "duration", required = true),
        @Validate(on = {"add", "save"}, field = "capacity", required = true, minvalue = 1),
        @Validate(on = {"add", "save"}, field = "description", required = true)
    })
    private TourDTO tour;
    private PackageTourDTO associatedTour;
    
    @EJBBean("java:comp/env/tourService")
    @EJB(name = "tourService", beanInterface = ITourService.class)
    private ITourService service;

    @EJBBean("java:comp/env/ptourService")
    @EJB(name = "ptourService", beanInterface = IPackageTourService.class)
    private IPackageTourService ptService;
    
    public List<IPackageTour> getPtours() {
        return ptService.findAll();
    }
    
    @Override
    public void setContext(ActionBeanContext context) {
        this.context = context;
    }

    @Override
    public ActionBeanContext getContext() {
        return context;
    }

    public TourDTO getTour() {
        return tour;
    }

    public void setTour(TourDTO tour) {
        this.tour = tour;
    }
    
    @Before(stages = LifecycleStage.BindingAndValidation, on = {"edit", "save"})
    public void loadTourFromDatabase() {
        String ids = context.getRequest().getParameter("tour.id");
        if (ids == null) {
            return;
        }
        tour = (TourDTO) service.getTour(Long.parseLong(ids));
    }

    @Before(stages = LifecycleStage.BindingAndValidation, on = {"add", "save"})
    public void loadAssociatedTour() {
        String ids = context.getRequest().getParameter("tour.associatedTour");
        if (ids == null) {
            return;
        }
        associatedTour = (PackageTourDTO) ptService.getPackageTour(Long.parseLong(ids));
    }
    
    public Resolution add() {
        tour.setAssociatedTour(associatedTour);
        service.createTour(tour);
        return new RedirectResolution("/ptour_show.jsp");
    }

    public Resolution delete() {
        service.deleteTour(tour);
        return new RedirectResolution("/ptour_show.jsp");
    }

    public Resolution edit() {
        return new ForwardResolution("/tour_edit.jsp");
    }

    public Resolution save() {
        tour.setAssociatedTour(associatedTour);
        service.updateTour(tour);
        return new RedirectResolution("/ptour_show.jsp");
    }

    public Resolution cancel() {
        return new ForwardResolution("/ptour_show.jsp");
    }
}
